package com.jiajia.study.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiajia.common.utils.PageUtils;
import com.jiajia.common.utils.Query;
import com.jiajia.study.product.dao.AttrGroupDao;
import com.jiajia.study.product.entity.AttrAttrgroupRelationEntity;
import com.jiajia.study.product.entity.AttrEntity;
import com.jiajia.study.product.entity.AttrGroupEntity;
import com.jiajia.study.product.enums.AttrTypeEnum;
import com.jiajia.study.product.service.AttrAttrgroupRelationService;
import com.jiajia.study.product.service.AttrGroupService;
import com.jiajia.study.product.service.AttrService;
import com.jiajia.study.product.vo.AttrGroupRelationVo;
import com.jiajia.study.product.vo.AttrGroupWithAttrVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    @Lazy
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
        if(StringUtils.isNotBlank(key)){
            queryWrapper.and(a->
                    a.eq("attr_group_id",key)
                            .or().like("attr_group_name",key)
            );
        }
        if(null != categoryId && categoryId != 0){
            queryWrapper.eq("catelog_id",categoryId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrId) {
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_group_id",attrId);
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(queryWrapper);

        List<Long> attrIds = list.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        if(!attrIds.isEmpty()){
           return attrService.listByIds(attrIds);
        }

        return null;
    }

    @Override
    public PageUtils attrNoRelation(Long attrGroupId, Map<String, Object> params) {

        // 1. 当前分组，只能关联自己所属分类里面的所有属性
        AttrGroupEntity attrGroup = this.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();

        // 2. 当前分组只能关联别的分组没有引用的属性
        // 2.1 当前分类下的其它分组
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catelogId);
//        queryWrapper.ne("attr_group_id",attrGroupId);
        List<AttrGroupEntity> currentCatelogOtherAttrGroup = this.list(queryWrapper);
        // 获取分组id
        List<Long> otherAttrGroupIds = currentCatelogOtherAttrGroup.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());

        QueryWrapper<AttrEntity> attrEntityQueryWrapper = new QueryWrapper<>();
        attrEntityQueryWrapper.eq("catelog_id",catelogId)
                .eq("attr_type", AttrTypeEnum.BASE_ATTR.getType());

        // 这些分组关联的属性
        QueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityQueryWrapper  = new QueryWrapper<>();
        if(otherAttrGroupIds != null && !otherAttrGroupIds.isEmpty()){
            attrAttrgroupRelationEntityQueryWrapper.in("attr_group_id",otherAttrGroupIds);
            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationService.list(attrAttrgroupRelationEntityQueryWrapper);
            List<Long> haveRelationAttrIds = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            if(null != haveRelationAttrIds && !haveRelationAttrIds.isEmpty()){
                attrEntityQueryWrapper.notIn("attr_id",haveRelationAttrIds);
            }
        }




        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            attrEntityQueryWrapper.and(a->a.like("attr_name",key).or().eq("attr_id",key));
        }

        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params),attrEntityQueryWrapper);
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }

    @Override
    public void saveRelation(List<AttrGroupRelationVo> attrGroupRelationVos) {

        List<AttrAttrgroupRelationEntity> relationEntities = attrGroupRelationVos.stream().map(item -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtil.copyProperties(item, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationService.saveBatch(relationEntities);
    }

    @Override
    public void deleteRelation(AttrGroupRelationVo[] relationVos) {
        List<AttrGroupRelationVo> list = Arrays.asList(relationVos);
        List<AttrAttrgroupRelationEntity> collect = list.stream().map(item -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtil.copyProperties(item, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationService.deleteBatchRelation(collect);
    }

    @Override
    public List<AttrGroupWithAttrVO> getAttrgroupWithAttrs(Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catelogId);
        List<AttrGroupEntity> list = this.list(queryWrapper);

        List<AttrGroupWithAttrVO> attrGroupWithAttrVOS = list.stream().map(item -> {
            AttrGroupWithAttrVO attrGroupWithAttrVO = new AttrGroupWithAttrVO();
            BeanUtil.copyProperties(item, attrGroupWithAttrVO);
            // 根据分组id,查询商品的分组属性
            List<AttrEntity> attrEntities = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupWithAttrVO.setAttrs(attrEntities);
            return attrGroupWithAttrVO;
        }).collect(Collectors.toList());

        return attrGroupWithAttrVOS;
    }

}
